<?php

namespace app\beautiful\controller\admin\partner;

use app\BaseController;
use app\beautiful\model\member\MemberModel;
use app\beautiful\model\order\GoodsOrderItemModel;
use app\beautiful\model\order\GoodsOrderModel;
use app\beautiful\model\partner\BonusModel;
use app\beautiful\model\partner\PartnerBonusModel;
use app\beautiful\model\partner\PartnerShareModel;
use app\beautiful\model\PartnerModel;
use app\beautiful\model\setting\ConsumeBonusSettingModel;
use app\beautiful\model\setting\PartnerBonusSettingModel;
use app\beautiful\model\setting\PartnerDividendSettingModel;
use app\beautiful\model\setting\PartnerTaskBonusSettingModel;
use app\common\logic\DataLogic;
use app\common\model\OrderModel;
use app\common\model\ShopDetailModel;


class PartnerBonus extends BaseController{

    /*分红进行中*/
    public function index()

    {

        $search = $this->request->param('search');
        $status = 1;//1进行中2已结束
        //判断开关
        $shop_detail = ShopDetailModel::where('shop_id',$this->request->shopId)->find();
        if($shop_detail->is_open_partner_manage<1){
            $return['list'] = $return['bonus'] = [];
            $return['count'] = 0;
            return $this->success($return);
        }

        $where['search']=$search;
        $where['status']=$status;
        $where['shop_id']=$this->request->shopId;
        $where['is_delete']=0;
        $pb_model = new PartnerBonusModel();
        $return = $pb_model::ulist($where,['total_ratio'=>'desc'],$this->request->page,$this->request->limit);

        //分红设置
        $bonus_setting = PartnerBonusSettingModel::where(['shop_id'=>$this->request->shopId,'is_delete'=>0])->find();
        if($bonus_setting->bonus_cycle_type == 1){
            $start_time = date('Y-m').'-01 00:00:00';
            $end_time = date('Y-m-t').' 23:59:59';
        }else if($bonus_setting->bonus_cycle_type == 2){
            //第一期的季度开始和结束时间
            $season = ceil((date('m'))/3);
            //当前季度开始时间戳
            $start_time  = date('Y-m-d H:i:s',mktime(0, 0, 0,$season*3-3+1,1,date('Y')));
            $end_time = date('Y-m-d H:i:s',mktime(23,59,59,$season*3,date('t',mktime(0, 0 , 0,$season*3,1,date("Y"))),date('Y')));

        }else{
            //天
            $start_time = date('Y-m-d').' 00:00:00';//开始的时间戳
            $end_time =date('Y-m-d').' 23:59:59';//结束的时间戳
        }

        $bp_where['shop_id'] = $this->request->shopId;
        $bp_where['start_time'] = $bonus_setting?strtotime($start_time):null;
        $bp_where['end_time'] = $bonus_setting?strtotime($end_time):null;
        //本期奖金
        //$bonus_pool = $return['list']?$return['list'][0]['all_reward_money']:0;
        $bonus_pool = partner_reward_money($bp_where);

        //分红股已经发放的份额
        $share_num = PartnerModel::where(['shop_id'=>$this->request->shopId,'is_delete'=>0,'status'=>1])->sum('bonus_share');

        //任务分红设置
        $task_bonus_setting = PartnerTaskBonusSettingModel::where(['shop_id'=>$this->request->shopId,'is_delete'=>0])->find();
        $join_role_ids = $task_bonus_setting['join_role_id']?explode(',',$task_bonus_setting['join_role_id']):[];
        $list = $return['list'];
        foreach ($list as $k=>$v){
            $partner = PartnerModel::where(['shop_id'=>$this->request->shopId,'partner_id'=>$v['partner_id'],'is_delete'=>0])->find();
            $list[$k]['is_in_task_bonus'] = in_array($partner['role_id'],$join_role_ids)?1:0;
        }
        $return['list'] = $list;
        $return['bonus'] = [
            'task_status'=>$task_bonus_setting?$task_bonus_setting->status:null,
            'start_time'=>$bonus_setting?$start_time:null,
            'end_time'=>$bonus_setting?$end_time:null,
            'bonus_pool'=>sprintf("%.2f",$bonus_pool),
            'total_share_num'=>$bonus_setting->total_share_num,//设置表为准
            'share_remand_qty'=>$bonus_setting->total_share_num - $share_num,//设置表为准
            'bonus_share_get_type'=>explode(',',$bonus_setting->bonus_share_get_type)//设置表为准
        ];

        return $this->success($return);

    }
    /*分红进行中详情*/
    public function bonusDetail(){
        $partner_bonus_id = $this->request->param('partner_bonus_id');

        $pb_model = new PartnerBonusModel();
        $field = 'bonus_id,total_money,cousume_bonus_ratio,toker_bonus_ratio,promotion_bonus_ratio,task_detail';
        $partner_bonus = $pb_model->getById(['partner_bonus_id'=>$partner_bonus_id,'is_delete'=>0],$field);
        if(null === $partner_bonus) return $this->error('详情不存在，参数有误');

        $partner_bonus['toker_total_price'] = $partner_bonus['task_detail']['toker_consume_complete'];
        $partner_bonus['promotion_total_num'] = $partner_bonus['task_detail']['promotion_share_complete'];

        $partner_bonus['set_consume_task_type'] = $partner_bonus['task_detail']['consume_task_type'];
        $partner_bonus['set_consume_price'] = $partner_bonus['task_detail']['consume_price'];
        $partner_bonus['set_consume_type'] = $partner_bonus['task_detail']['consume_type'];
        $partner_bonus['set_consume_bonus_ratio'] = $partner_bonus['task_detail']['consume_bonus_ratio'];

        $partner_bonus['set_toker_task_type'] = $partner_bonus['task_detail']['toker_task_type'];
        $partner_bonus['set_toker_consume_price'] = $partner_bonus['task_detail']['toker_consume_price'];
        $partner_bonus['set_toker_consume_type'] = $partner_bonus['task_detail']['toker_consume_type'];
        $partner_bonus['set_toker_bonus_ratio'] = $partner_bonus['task_detail']['toker_bonus_ratio'];

        $partner_bonus['set_promotion_task_type'] = $partner_bonus['task_detail']['promotion_task_type'];
        $partner_bonus['set_promotion_share_num'] = $partner_bonus['task_detail']['promotion_share_num'];
        $partner_bonus['set_promotion_bonus_ratio'] = $partner_bonus['task_detail']['promotion_bonus_ratio'];

        $partner_bonus['consume_task_type'] = $partner_bonus['task_detail']['consume_task_type'];
        $partner_bonus['toker_task_type'] = $partner_bonus['task_detail']['toker_task_type'];
        $partner_bonus['promotion_task_type'] = $partner_bonus['task_detail']['promotion_task_type'];

        unset($partner_bonus['task_detail']);


        return $this->success($partner_bonus);
    }
    /*历史分红*/
    public function historyBonus(){
        $where=[

            ['shop_id','=',$this->request->shopId],

            ['is_delete','=',0],

            ['is_end','=',1],

        ];

        $bonus_model= new BonusModel();

        $order=['add_time'=>'desc'];

        $field="*,bonus_pool-bonus_total_price as return_price";

        $append=[];

        $return = DataLogic::getDataList($bonus_model,$where,$order,$field,$this->request->limit,$this->request->page,$append);

        return $this->success($return);
    }
    /*历史分红详情*/
    public function historyBonusDetail(){
        $bonus_id = $this->request->param('bonus_id');
        //分红
        if(null === $bonus = BonusModel::where(['shop_id'=>$this->request->shopId,'is_delete'=>0,'bonus_id'=>$bonus_id])->find()){
            return $this->error('分红不存在');
        }
        $join_task_bonus_role_id = $bonus->join_task_bonus_role_id?explode(',',$bonus->join_task_bonus_role_id):[];
        $where['bonus_id'] = $bonus_id;
        $where['status'] = 2;
        $pb_model = new PartnerBonusModel();
        $return = $pb_model::ulist($where,['total_ratio'=>'desc'],$this->request->page,$this->request->limit,$join_task_bonus_role_id,$bonus->task_status);

        $return['bonus'] = [
            'start_time'=>$bonus->start_time,
            'end_time'=>$bonus->end_time,
        ];
        return $this->success($return);
    }






    public function indexOld()

    {

        $search=$this->request->param('search');

        //分红设置
        $bonus_setting = PartnerBonusSettingModel::where(['shop_id'=>$this->request->shopId,'is_delete'=>0])->find();

        //分红
        $bonus = BonusModel::where(['shop_id'=>$this->request->shopId,'is_delete'=>0,'is_end'=>0])->find();

        $where=[

            ['shop_id','=',$this->request->shopId],

            ['is_delete','=',0],

            ['status','=',1],

            ['type','in',explode(',',$bonus_setting->join_bonus_role_id)]

        ];

        if(!empty($search)) $where[]=['name|mobile','like','%'.$search.'%'];

        $partner_model = new PartnerModel();

        $partner_model = $partner_model->whereTime('expire_time','>=',date('Y-m-d'));

        $order=['add_time'=>'desc'];

        $field="*";

        $append=[];

        $return = DataLogic::getDataList($partner_model,$where,$order,$field,$this->request->limit,$this->request->page,$append);

        $list = $return['list'];

        foreach ($list as $k=>$v){
            $task_finish_ratio = 0;
            $task_bonus_setting = PartnerTaskBonusSettingModel::where(['shop_id'=>$this->request->shopId,'is_delete'=>0])->find();

            if($task_bonus_setting->status==1){
                //消费任务进度
                $cousume_finish_ratio = $this->cousume_ratio($task_bonus_setting,$v,$bonus);

                //拓客任务进度
                $toker_finish_ratio = $this->toker_ratio($task_bonus_setting,$v,$bonus);

                //推广任务进度
                $promotion_finish_ratio = $this->promotion_ratio($task_bonus_setting,$v,$bonus);

                $task_finish_ratio = $cousume_finish_ratio + $toker_finish_ratio + $promotion_finish_ratio;
            }

            $list[$k]['task_finish_ratio'] = $task_finish_ratio;

        }
        //分红池

        if($bonus_setting->bonus_budget_type==1){
            $total_need_pay = GoodsOrderModel::where(['shop_id'=>$this->request->shopId,'is_delete'=>0,'is_paid'=>1])
                ->whereTime('pay_time','>=',strtotime($bonus->start_time))
                ->whereTime('pay_time','<=',strtotime($bonus->end_time))
                ->sum('need_pay');
            $bonus_pool = $total_need_pay * ($bonus_setting->bonus_budget/100);

        }else{
            $order_ids= GoodsOrderModel::where(['shop_id'=>$this->request->shopId,'is_delete'=>0,'is_paid'=>1])
                ->whereTime('pay_time','>=',strtotime($bonus->start_time))
                ->whereTime('pay_time','<=',strtotime($bonus->end_time))
                ->column('order_id');
            $total_goods_gross = GoodsOrderItemModel::where(['shop_id'=>$this->request->shopId])
                ->whereIn('order_id',$order_ids)
                ->sum('goods_gross');
            $bonus_pool = $total_goods_gross * ($bonus_setting->bonus_budget/100);
        }

        //分红股已经发放的份额
        $share_num = PartnerModel::where(['shop_id'=>$this->request->shopId,'is_delete'=>0,'status'=>1])->sum('bonus_share');
        $return['bonus'] = [
            'start_time'=>$bonus->start_time,
            'end_time'=>$bonus->end_time,
            'bonus_pool'=>$bonus_pool,
            'total_share_num'=>$bonus_setting->total_share_num,
            'share_remand_qty'=>$bonus_setting->total_share_num - $share_num,
            'bonus_share_get_type'=>explode(',',$bonus_setting->bonus_share_get_type)
        ];
        $return['list'] = $list;

        return $this->success($return);

    }
    /*分红进行中详情*/
    public function bonusDetailOld(){
        $partner_id = $this->request->param('partner_id');

        if(null === $partner = PartnerModel::where(['shop_id'=>$this->request->shopId,'is_delete'=>0,'partner_id'=>$partner_id])->find()) return $this->error('股东不存在');
        //分红
        if(null === $task_bonus_setting = PartnerTaskBonusSettingModel::where(['shop_id'=>$this->request->shopId,'is_delete'=>0])->find()) return $this->error('任务分红未设置');

        if(null === $bonus = BonusModel::where(['shop_id'=>$this->request->shopId,'is_delete'=>0,'is_end'=>0])->find()) return $this->error('没有进行中的分红');

        if($task_bonus_setting->status==1){
            //消费任务
            $total_cousume_price = $this->cousumePrice($task_bonus_setting,$bonus,$partner);
            $cousume_task = [
                'consume_task_type'=>$task_bonus_setting->consume_task_type,

                'consume_bonus_ratio'=>$task_bonus_setting->consume_bonus_ratio,

                'consume_price'=>$task_bonus_setting->consume_price,

                'total_cousume_price'=>$total_cousume_price,

                'is_complete'=>$total_cousume_price>=$task_bonus_setting->consume_price?1:0,

                'get_ratio'=>$total_cousume_price>=$task_bonus_setting->consume_price?$task_bonus_setting->consume_bonus_ratio:0,
            ];

            //拓客任务
            $total_cousume_price = $this->tokerCousumePrice($task_bonus_setting,$bonus,$partner);

            $toker_task = [
                'toker_task_type'=>$task_bonus_setting->toker_task_type,

                'toker_bonus_ratio'=>$task_bonus_setting->toker_bonus_ratio,

                'toker_consume_price'=>$task_bonus_setting->toker_consume_price,

                'total_cousume_price'=>$total_cousume_price,

                'is_complete'=>$total_cousume_price>=$task_bonus_setting->toker_consume_price?1:0,

                'get_ratio'=>$total_cousume_price>=$task_bonus_setting->toker_consume_price?$task_bonus_setting->toker_bonus_ratio:0,
            ];

            //推广任务
            $total_share_count = $this->promotionCount($task_bonus_setting,$bonus,$partner);

            $promotion_task = [
                'promotion_task_type'=>$task_bonus_setting->promotion_task_type,

                'promotion_bonus_ratio'=>$task_bonus_setting->promotion_bonus_ratio,

                'promotion_share_num'=>$task_bonus_setting->promotion_share_num,

                'total_share_count'=>$total_share_count,

                'is_complete'=>$total_cousume_price>=$task_bonus_setting->toker_consume_price?1:0,

                'get_ratio'=>$total_share_count>=$task_bonus_setting->promotion_share_num?$task_bonus_setting->promotion_bonus_ratio:0,
            ];
            $return['detail'] = [
                'cousume_task'=>$cousume_task,
                'toker_task'=>$toker_task,
                'promotion_task'=>$promotion_task
            ];

        }else{
            $return['detail'] = [];
        }


        return $this->success($return);
    }
    /*消费任务完成率*/
    public function cousume_ratio($task_bonus_setting,$partner,$bonus){
        $cousume_finish_ratio = 0;

        if($task_bonus_setting->consume_task_type<1){
            //无消费任务
            $cousume_finish_ratio = $task_bonus_setting->consume_bonus_ratio;
        }else{
            if($task_bonus_setting->consume_type==1){

                //单次金额满足
                $order= GoodsOrderModel::where(['shop_id'=>$this->request->shopId,' is_delete'=>0,'member_id'=>$partner['member_id'],'is_paid'=>1])
                    ->where('need_pay','>=',$task_bonus_setting->consume_price)
                    ->whereTime('pay_time','>=',strtotime($bonus->start_time))
                    ->whereTime('pay_time','<=',strtotime($bonus->end_time))
                    ->find();

                if($order){
                    $cousume_finish_ratio = $task_bonus_setting->consume_bonus_ratio;
                }
            }else{
                //累计金额满足
                $total_cousume_price = GoodsOrderModel::where(['shop_id'=>$this->request->shopId,'is_delete'=>0,'member_id'=>$partner['member_id'],'is_paid'=>1])
                                    ->whereTime('pay_time','>=',strtotime($bonus->start_time))
                                    ->whereTime('pay_time','<=',strtotime($bonus->end_time))
                                    ->sum('need_pay');

                if($total_cousume_price>=$task_bonus_setting->consume_price){

                    $cousume_finish_ratio = $task_bonus_setting->consume_bonus_ratio;
                }
            }


        }

        return $cousume_finish_ratio;
    }
    /*拓客任务完成率*/
    public function toker_ratio($task_bonus_setting,$partner,$bonus){
        $toker_finish_ratio = 0;

        if($task_bonus_setting->toker_task_type<1){
            //无消费任务
            $toker_finish_ratio = $task_bonus_setting->toker_finish_ratio;
        }else{

            //客户列表
            $member_ids = MemberModel::where(['shop_id'=>$this->request->shopId,'is_delete'=>0,'status'=>1,'partner_id'=>$partner['partner_id']])->column('member_id');

            $order_model = new GoodsOrderModel();

            if($task_bonus_setting->toker_consume_type==1){
                //单次金额满足
                $order= $order_model->alias('o')
                    ->leftJoin('beautiful_member m','o.member_id=m.member_id and o.pay_time>=m.partner_bind_time')
                    ->where(['o.shop_id'=>$this->request->shopId,'o.is_delete'=>0,'o.is_paid'=>1])
                    ->where('o.need_pay','>=',$task_bonus_setting->toker_consume_price)
                    ->whereTime('o.pay_time','>=',strtotime($bonus->start_time))
                    ->whereTime('o.pay_time','<=',strtotime($bonus->end_time))
                    ->whereIn('m.member_id',$member_ids)
                    ->find();
                if($order){
                    $toker_finish_ratio = $task_bonus_setting->toker_bonus_ratio;
                }
            }else{
                //累计金额满足
                $total_cousume_price= $order_model->alias('o')
                    ->leftJoin('beautiful_member m','o.member_id=m.member_id and o.pay_time>=m.partner_bind_time')
                    ->where(['o.shop_id'=>$this->request->shopId,'o.is_delete'=>0,'o.is_paid'=>1])
                    ->where('o.need_pay','>=',$task_bonus_setting->toker_consume_price)
                    ->whereTime('o.pay_time','>=',strtotime($bonus->start_time))
                    ->whereTime('o.pay_time','<=',strtotime($bonus->end_time))
                    ->whereIn('m.member_id',$member_ids)
                    ->sum('o.need_pay');

                if($total_cousume_price>=$task_bonus_setting->toker_consume_price){

                    $toker_finish_ratio = $task_bonus_setting->toker_bonus_ratio;
                }
            }


        }

        return $toker_finish_ratio;
    }
    /*推广任务完成率*/
    public function promotion_ratio($task_bonus_setting,$partner,$bonus){
        $promotion_finish_ratio = 0;
        if($task_bonus_setting->toker_task_type<1) {
            //无消费任务
            $promotion_finish_ratio = $task_bonus_setting->promotion_bonus_ratio;
        }else{
            //分享次数
            //新用户
            $new_share_count = PartnerShareModel::where(['shop_id'=>$this->request->shopId,'is_delete'=>0,'partner_id'=>$partner['partner_id'],'is_new'=>1])
                ->whereTime('add_time','>=',$bonus->start_time)
                ->whereTime('add_time','<=',$bonus->end_time)
                ->count();
            if($task_bonus_setting->is_memeber_once){
                //同一用户多次分享算一次
                //老用户
                $old_share_count = 0;
            }else{
                //同一用户多次分享算多次
                //老用户
                $old_share_count = PartnerShareModel::where(['shop_id'=>$this->request->shopId,'is_delete'=>0,'partner_id'=>$partner['partner_id'],'is_new'=>0,'is_receive'=>1])
                    ->whereTime('add_time','>=',$bonus->start_time)
                    ->whereTime('add_time','<=',$bonus->end_time)
                    ->count();
            }
            $total_share_count = $new_share_count + $old_share_count;
            if($total_share_count>=$task_bonus_setting->promotion_share_num){
                $promotion_finish_ratio = $task_bonus_setting->promotion_bonus_ratio;
            }
        }


        return $promotion_finish_ratio;
    }
    /*历史任务分红详情*/
    public function historyBonusDetailOld(){
        $bonus_id = $this->request->param('bonus_id');
        //分红
        if(null === $bonus = BonusModel::where(['shop_id'=>$this->request->shopId,'is_delete'=>0,'bonus_id'=>$bonus_id])->find()){
            return $this->error('分红不存在');
        }

        $where=[

            ['shop_id','=',$this->request->shopId],

            ['is_delete','=',0],

            ['bonus_id','=',$bonus_id],

        ];

        $partner_bonus_model = new PartnerBonusModel();

        $partner_bonus_model = $partner_bonus_model::with(['partner'=>function($query){
            $query->field('partner_id,name,mobile,member_id,role_id');
        }]);
        $order=['add_time'=>'desc'];

        $field="*,cousume_bonus_ratio+toker_bonus_ratio+promotion_bonus_ratio as task_finish_ratio";

        $append=[];

        $return = DataLogic::getDataList($partner_bonus_model,$where,$order,$field,$this->request->limit,$this->request->page,$append);

        $return['bonus'] = [
            'start_time'=>$bonus->start_time,
            'end_time'=>$bonus->end_time,
        ];
        return $this->success($return);
    }
    /*消费金额统计*/
    public function cousumePrice($task_bonus_setting,$bonus,$partner){
        $total_cousume_price = 0;
        if($task_bonus_setting->consume_type==1){
            //单次
            $order= GoodsOrderModel::where(['shop_id'=>$this->request->shopId,' is_delete'=>0,'member_id'=>$partner['member_id'],'is_paid'=>1])
                ->where('need_pay','>=',$task_bonus_setting->consume_price)
                ->whereTime('pay_time','>=',strtotime($bonus->start_time))
                ->whereTime('pay_time','<=',strtotime($bonus->end_time))
                ->order('pay_time asc')
                ->find();
            if($order){
                $total_cousume_price = $order->need_pay;
            }
        }else{
            //累计
            $total_cousume_price = GoodsOrderModel::where(['shop_id'=>$this->request->shopId,'is_delete'=>0,'member_id'=>$partner['member_id'],'is_paid'=>1])
                ->whereTime('pay_time','>=',strtotime($bonus->start_time))
                ->whereTime('pay_time','<=',strtotime($bonus->end_time))
                ->sum('need_pay');

        }
        return $total_cousume_price;
    }
    /*拓客消费金额统计*/
    public function tokerCousumePrice($task_bonus_setting,$bonus,$partner){
        $total_cousume_price = 0;
        //客户列表
        $member_ids = MemberModel::where(['shop_id'=>$this->request->shopId,'is_delete'=>0,'status'=>1,'partner_id'=>$partner['partner_id']])->column('member_id');

        $order_model = new GoodsOrderModel();

        if($task_bonus_setting->toker_consume_type==1){
            //单次金额满足
            $order= $order_model->alias('o')
                ->leftJoin('beautiful_member m','o.member_id=m.member_id and o.pay_time>=m.partner_bind_time')
                ->where(['o.shop_id'=>$this->request->shopId,'o.is_delete'=>0,'o.is_paid'=>1])
                ->where('o.need_pay','>=',$task_bonus_setting->toker_consume_price)
                ->whereTime('o.pay_time','>=',strtotime($bonus->start_time))
                ->whereTime('o.pay_time','<=',strtotime($bonus->end_time))
                ->whereIn('m.member_id',$member_ids)
                ->order('pay_time asc')
                ->find();
            if($order){
                $total_cousume_price = $order->need_pay;
            }
        }else{
            //累计金额满足
            $total_cousume_price= $order_model->alias('o')
                ->leftJoin('beautiful_member m','o.member_id=m.member_id and o.pay_time>=m.partner_bind_time')
                ->where(['o.shop_id'=>$this->request->shopId,'o.is_delete'=>0,'o.is_paid'=>1])
                ->where('o.need_pay','>=',$task_bonus_setting->toker_consume_price)
                ->whereTime('o.pay_time','>=',strtotime($bonus->start_time))
                ->whereTime('o.pay_time','<=',strtotime($bonus->end_time))
                ->whereIn('m.member_id',$member_ids)
                ->sum('o.need_pay');

        }
        return $total_cousume_price;
    }
    /*推广分享次数*/
    public function promotionCount($task_bonus_setting,$bonus,$partner){
        $total_share_count = 0;
        if($task_bonus_setting->toker_task_type<1) {

        }else{
            //分享次数
            //新用户
            $new_share_count = PartnerShareModel::where(['shop_id'=>$this->request->shopId,'is_delete'=>0,'partner_id'=>$partner['partner_id'],'is_new'=>1])
                ->whereTime('add_time','>=',$bonus->start_time)
                ->whereTime('add_time','<=',$bonus->end_time)
                ->count();
            if($task_bonus_setting->is_memeber_once){
                //同一用户多次分享算一次
                //老用户
                $old_share_count = 0;
            }else{
                //同一用户多次分享算多次
                //老用户
                $old_share_count = PartnerShareModel::where(['shop_id'=>$this->request->shopId,'is_delete'=>0,'partner_id'=>$partner['partner_id'],'is_new'=>0,'is_receive'=>1])
                    ->whereTime('add_time','>=',$bonus->start_time)
                    ->whereTime('add_time','<=',$bonus->end_time)
                    ->count();
            }
            $total_share_count = $new_share_count + $old_share_count;

        }
        return $total_share_count;
    }

}

